#include"stdafx.h"
#include "Parser.h"

Parser::Parser(){
}
LogicProcess* Parser::processCommand(string cmd,allTaskList *givenLists,allCommandStack *givenStacks){
	commandString=cmd;
	allTask=givenLists;
	prevCmds=givenStacks;

	string command;
	string content;

	command=takeFirstWord(commandString);
	content=takeContent(commandString,command);

	cmdType=detectCommandType(command);
	Processor=solveTheCommand(cmdType,content);

	return Processor;

}
string Parser::takeContent(string commandString,string command){
	string result;
	int commandLength=0;
	int cmdLength=0;
	commandLength=command.length();
	cmdLength=commandString.length();

	if (commandLength<cmdLength){
		result=commandString.substr(commandLength+1,cmdLength-commandLength-1);//exculde the space
	}else
		result=command;

	return result;
}
LogicProcess* Parser::solveTheCommand(commandType type,string content){
	LogicProcess *tmpProcessor;
	switch(type){
	case Add:
		tmpProcessor=new AddProcess(content,allTask,prevCmds);
		
		break;
	case Delete:
		tmpProcessor=new DeleteProcess(content,allTask,prevCmds);
		//processor->HandleProcesss();
		break;
	case Undo:
		tmpProcessor=new UndoProcess(content,allTask,prevCmds,1);
		break;
	case Redo:
		tmpProcessor=new RedoProcess(content,allTask,prevCmds,1);
		break;
	case Search:
		tmpProcessor=new SearchProcess(content,allTask,prevCmds);
		break;
	case Select:
		break;
	case Update:
		tmpProcessor=new UpdateProcess(content,allTask,prevCmds);
		//processor->HandleProcesss();
		break;
	case Exit:
		break;
	case Edit:
		tmpProcessor=new EditProcess(content,allTask,prevCmds);
		break;
	default:
		break;

	}

	return tmpProcessor;
}
string Parser::takeFirstWord(string tmp){
	string result;
	for (int i=0;i<tmp.length();i++){
		if (tmp[i]==' ')
			break;
		else
			result+=tmp[i];
	}
	return result;
}
commandType Parser::detectCommandType(string command){
	commandType result=Search;

	if (command==ADD)
		result=Add;
	if (command==DELETE)
		result=Delete;
	if (command==EDIT)
		result=Edit;
	if (command==UDATE)
		result=Update;
	if (command==UNDO)
		result=Undo;
	if (command==REDO)
		result=Redo;
	if (command==EXIT)
		result=Exit;

	return result;
}

commandType Parser::getCommandType(){
	return cmdType;
}